package com.quectel.core.module.group.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.core.module.group.dao.SysGroupDeviceDao;
import com.quectel.core.module.group.dto.SysGroupDeviceDto;
import com.quectel.core.module.group.entity.SysGroupDeviceEntity;
import com.quectel.core.module.group.service.SysGroupDeviceService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:13:13
 */
@DubboService
public class SysGroupDeviceServiceImpl implements SysGroupDeviceService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGroupDeviceServiceImpl.class);

    @Autowired
    private SysGroupDeviceDao sysGroupDeviceDao;


    @Override
    public void deleteByGroupId(Long groupId) {
        sysGroupDeviceDao.delete(new QueryWrapper<SysGroupDeviceEntity>().eq("sys_group_id", groupId));
    }

    @Override
    public void deleteByGroupIdAndDeviceId(Long groupId, Long deviceId) {
        sysGroupDeviceDao.delete(new QueryWrapper<SysGroupDeviceEntity>()
                .eq("sys_group_id", groupId).eq("device_id", deviceId));
    }

    @Override
    public List<SysGroupDeviceDto> queryList(Map<String, Object> params) {
        List<SysGroupDeviceEntity> list = sysGroupDeviceDao.queryList(params);
        return CopyUtils.copyList(list, SysGroupDeviceDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return sysGroupDeviceDao.queryTotal(params);
    }

    @Override
    public Long save(SysGroupDeviceDto dto) {
        SysGroupDeviceEntity entity = CopyUtils.copyObj(dto, SysGroupDeviceEntity.class);
        entity.setId(Snowflake.nextId());
        sysGroupDeviceDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SysGroupDeviceDto dto) {
        SysGroupDeviceEntity entity = CopyUtils.copyObj(dto, SysGroupDeviceEntity.class);
        sysGroupDeviceDao.updateById(entity);
    }

    @Override
    public SysGroupDeviceDto selectById(Long id) {
        SysGroupDeviceEntity entity = sysGroupDeviceDao.selectById(id);
        return CopyUtils.copyObj(entity, SysGroupDeviceDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysGroupDeviceDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Long> selectByGroupId(Long sysGroupId) {
        List<SysGroupDeviceEntity> sysGroupDeviceEntitys =
                sysGroupDeviceDao.selectList(new LambdaQueryWrapper<SysGroupDeviceEntity>().eq(SysGroupDeviceEntity::getSysGroupId, sysGroupId));
        return sysGroupDeviceEntitys.stream().map(SysGroupDeviceEntity::getDeviceId).collect(Collectors.toList());
    }

    @Override
    public List<SysGroupDeviceDto> selectListByDeviceId(Long id) {
        List<SysGroupDeviceEntity> sysGroupDeviceEntitys = sysGroupDeviceDao.selectList(new QueryWrapper<SysGroupDeviceEntity>().eq("device_id", id));
        return CopyUtils.copyList(sysGroupDeviceEntitys, SysGroupDeviceDto.class);
    }
}
